package cz.data.domain.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.system.mapstruct.QrtzJobMapper;
import cz.data.domain.system.model.dto.QrtzJobDto;
import cz.data.domain.system.model.entity.QrtzJobEntity;
import cz.data.domain.system.model.query.QrtzJobQuery;
import cz.data.domain.system.model.vo.QrtzJobVo;
import cz.data.domain.system.service.QrtzJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"定时任务"})
@RestController
public class QrtzJobController extends BaseController {

    @Autowired
    private QrtzJobService qrtzJobService;

    @Autowired
    private QrtzJobMapper qrtzJobMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/quartz/jobs/{id}")
    public SimpleDataResponse<QrtzJobVo> getQrtzJobById(@PathVariable String id) {
        QrtzJobEntity qrtzJobEntity = qrtzJobService.getQrtzJobById(id);
        return new SimpleDataResponse<>(qrtzJobMapper.toVO(qrtzJobEntity));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "定时任务-列表")
    @GetMapping("/quartz/jobs/list")
    public TableDataResponse<QrtzJobVo> getQrtzJobList() {
        QueryWrapper<QrtzJobEntity> queryWrapper = new QueryWrapper<>();
        List<QrtzJobEntity> list = qrtzJobService.list(queryWrapper);
        List<QrtzJobVo> collect = list.stream().map(qrtzJobMapper::toVO).collect(Collectors.toList());
        return new TableDataResponse<>(collect);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "定时任务-分页查询")
    @ApiImplicitParam(name = "qrtzJobQuery", value = "查询实体qrtzJobQuery", required = true, dataTypeClass = QrtzJobQuery.class)
    @GetMapping("/quartz/jobs/page")
    public SimpleDataResponse<JsonPage<QrtzJobVo>> getQrtzJobPage(QrtzJobQuery qrtzJobQuery) {
        QueryWrapper<QrtzJobEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(qrtzJobQuery.getJobName()), "job_name", qrtzJobQuery.getJobName());
        IPage<QrtzJobEntity> page = qrtzJobService.page(new Page<>(qrtzJobQuery.getPageNum(), qrtzJobQuery.getPageSize()), queryWrapper);
        List<QrtzJobVo> collect = page.getRecords().stream().map(qrtzJobMapper::toVO).collect(Collectors.toList());
        JsonPage<QrtzJobVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "定时任务-新增", notes = "根据qrtzJob对象添加信息")
    @ApiImplicitParam(name = "qrtzJob", value = "详细实体qrtzJob", required = true, dataType = "QrtzJobDto")
    @PostMapping("/quartz/jobs")
    public BaseResponse saveQrtzJob(@RequestBody @Validated({ValidationGroups.Insert.class}) QrtzJobDto qrtzJob) {
        qrtzJobService.saveQrtzJob(qrtzJob);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "定时任务-修改", notes = "根据url的id来指定修改对象，并根据传过来的信息来修改详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "qrtzJob", value = "详细实体qrtzJob", required = true, dataType = "QrtzJobDto")
    })
    @PutMapping("/quartz/jobs/{id}")
    public BaseResponse updateQrtzJob(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) QrtzJobDto qrtzJob) {
        qrtzJobService.updateQrtzJob(qrtzJob);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "定时任务-删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/quartz/jobs/{id}")
    public BaseResponse deleteQrtzJobById(@PathVariable String id) {
        qrtzJobService.deleteQrtzJobById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "定时任务-暂停任务", notes = "根据url的id来暂停指定任务")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @PostMapping("/quartz/jobs/pause/{id}")
    public BaseResponse pauseById(@PathVariable("id") String id) {
        qrtzJobService.pauseById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "定时任务-恢复任务", notes = "根据url的id来恢复指定任务")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @PostMapping("/quartz/jobs/resume/{id}")
    public BaseResponse resumeById(@PathVariable("id") String id) {
        qrtzJobService.resumeById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "定时任务-立即执行任务")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @PostMapping("/run/{id}")
    public BaseResponse runById(@PathVariable("id") String id) {
        qrtzJobService.runById(id);
        return BaseResponse.success();
    }
}
